<template>
	<!-- 用户管理-分配部门 -->
	<v-dialog v-model="setDepartmentModel.departmentDialog" width="600">
		<v-card>
			<v-card-title>分配部门</v-card-title>
			<v-card-text>
				<v-treeview item-disabled="locked" v-model="selectionDepartments" :items="departmentItems" item-key="id"
					return-object activatable open-on-click dense>
					<template v-slot:prepend="{ item, open }">
						<v-radio-group v-model="currentSelectDepartId" dense>
							<v-radio v-model="item.id" :key="item.id"></v-radio>
						</v-radio-group>
					</template>
				</v-treeview>

			</v-card-text>
			<v-card-actions>
				<v-spacer></v-spacer>
				<v-btn v-if="setDepartmentModel.getSelectDepartmentDialog" color="success" class="mr-4" text
					@click="callBackSetSelectDepartmentId(currentSelectDepartId),setDepartmentModel.departmentDialog=false">
					确认</v-btn>
				<v-btn v-else color="success" class="mr-4" text @click="setDepartmentSubmit">确认</v-btn>
				<v-btn color="warning" class="mr-4" text @click="close">取消</v-btn>

			</v-card-actions>
		</v-card>
	</v-dialog>
</template>


<script>
	export default {
		name: 'set_department',
		props: {
			callBackLoadUsers: {
				type: Function, //参数类型：函数
			},
			callBackSetSelectDepartmentId: {
				type: Function, //参数类型：函数
			},
			callBackSetSelectDepartmentModel: {
				type: Function, //参数类型：函数
			},
			setDepartmentModel: {
				departmentDialog: false,
				userIdList: [],
				//只获取选中部门的id,不与后台交互
				getSelectDepartmentDialog: false,
				GetDepartmentByIdUrl: ``
			},
		},
		data: () => ({
			departmentItems: [],
			selectionDepartments: [],
			currentSelectDepartId: 0
		}),
		watch: {
			setDepartmentModel: {
				handler(newVal) {
					this.$emit('update:setDepartmentModel', newVal)

					this.loadDepartmentList()
				},
				deep: true
			},
		},

		methods: {
			async loadDepartmentList() {

				if (!this.setDepartmentModel.departmentDialog) {
					return
				}

				var response = await this.$httpRequest({
					url: `SPM/User/GetDepartmentTree`,
					method: 'get'
				})

				if (response.status == 1) {
					this.departmentItems = response.data.departments
				} else {
					this.$message.error(response.info)
				}
			},
			async setDepartmentSubmit() {
				var response = await this.$httpRequest({
					url: `SPM/User/SetUsersDepartMent?departMentId=${this.currentSelectDepartId}`,
					method: 'post',
					data: this.setDepartmentModel.userIdList
				})

				if (response.status == 1) {
					this.$message.success(response.info)
					this.$props.callBackLoadUsers()
					this.close()
				} else {
					this.$message.error(response.info)
				}
			},
			async getDepartmentModelById(depId) {
				var response = await this.$httpRequest({
					url: this.setDepartmentModel.GetDepartmentByIdUrl+`id=${depId}`,
					method: 'get'
				})
				return response.data
			},
			setDepartModel(depId) {
				var model = this.getDepartmentModelById(depId)
				this.$props.callBackSetSelectDepartmentModel(model)
			},
			close() {
				this.currentSelectDepartId = 0
				this.setDepartmentModel.departmentDialog = false

			},
		}
	}
</script>

<style>
</style>
